using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Drawing2D;
using System.Runtime.Serialization.Formatters.Binary;

namespace fsapp
{
    public partial class Form1 : Form
    {
        private SchemeComponent sc, sc1;
        private ComponentContainer cc;
        private Scheme Sch;
        int InitW, InitH;
        public Form1()
        {
            InitializeComponent();
            InitH = pictureBox1.Height;
            InitW = pictureBox1.Width;
//            CreateDynamic();
            FileStream fs = File.OpenRead("testscheme.fsc");
            BinaryFormatter bf = new BinaryFormatter();
            Sch = (Scheme)bf.Deserialize(fs);
            fs.Close();
            Sch.CreateNonSerializedObjects(pictureBox1);
            Sch.SetScrollMode(hScrollBar1, vScrollBar1);

        }

        private void CreateDynamic()
        {
            MemoryStream pd = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(pd);
            bw.Write((int)PrimitiveType.FillRectangle);
            bw.Write((int)9);
            bw.Write((int)9);
            bw.Write((int)40);
            bw.Write((int)140);
            bw.Write((int)PrimitiveType.Line);
            bw.Write((int)2);
            bw.Write((int)135);
            bw.Write((int)9);
            bw.Write((int)135);
            bw.Write((int)PrimitiveType.Line);
            bw.Write((int)2);
            bw.Write((int)20);
            bw.Write((int)9);
            bw.Write((int)20);
            bw.Write((int)PrimitiveType.Line);
            bw.Write((int)49);
            bw.Write((int)135);
            bw.Write((int)56);
            bw.Write((int)135);
            bw.Write((int)PrimitiveType.Line);
            bw.Write((int)49);
            bw.Write((int)20);
            bw.Write((int)56);
            bw.Write((int)20);


            MemoryStream pd1 = new MemoryStream();
            BinaryWriter bw1 = new BinaryWriter(pd1);
            bw1.Write((int)PrimitiveType.FillEllipse);
            bw1.Write((int)2);
            bw1.Write((int)2);
            bw1.Write((int)50);
            bw1.Write((int)50);

            Sch = new Scheme();
            Sch.CreateNonSerializedObjects(pictureBox1);
            sc = new SchemeComponent(pd, 58, 158);
            sc1 = new SchemeComponent(pd1, 54, 54);
            PointF Contact = new PointF(2, 20);
            sc.Contacts.Add(Contact);
            PointF Contact1 = new PointF(2, 135);
            sc.Contacts.Add(Contact1);
            PointF Contact2 = new PointF(56, 135);
            sc.Contacts.Add(Contact2);
            PointF Contact3 = new PointF(56, 20);
            sc.Contacts.Add(Contact3);
            //cc.Angel = 45;
            Sch.Width = 2000;
            Sch.Height = 2000;
            Sch.Scale = 1.4f;
            cc = Sch.AddContainer(sc);
            cc.X = 20;
            cc.Y = 20;

            Random r = new Random(1);
            for (int i = 1; i <= 50; i++)
            {
                cc = Sch.AddContainer(sc);
                cc.X = r.Next(2000 - 60);
                cc.Y = r.Next(2000 - 170);
            }
            /*            for (int i = 1; i <= 20; i++)
                        {
                            cc = Sch.AddContainer(sc1);
                            cc.X = r.Next(2000);
                            cc.Y = r.Next(2000);
                        }
            */
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (this.ClientSize.Width > pictureBox1.Width)
            { 
                pictureBox1.Width = InitW;
                pictureBox1.Left = (this.ClientSize.Width - pictureBox1.Width) / 2;
            }
            else 
            {
                pictureBox1.Left = 0;
                pictureBox1.Width = this.ClientSize.Width;
            }
            if (this.ClientSize.Height > pictureBox1.Height)
            {
                pictureBox1.Height = InitH;
                pictureBox1.Top = (this.ClientSize.Height - pictureBox1.Height) / 2;
            }
            else
            {
                pictureBox1.Top = 0;
                pictureBox1.Height = this.ClientSize.Height;
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            BinaryFormatter bf = new BinaryFormatter();
            Stream fs = new FileStream("testscheme.fsc", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
            bf.Serialize(fs, Sch);
            fs.Close();
        }


    }
}